package com.fzj.example.thread.executors;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * ClassName: ScheduledThreadPoolTest
 * date: 2019/10/25 15:01
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class ScheduledThreadPoolTest {

    public static Map<String,Object> objectMap=new ConcurrentHashMap<>();

    public static void main(String[] args) throws IOException {
        //获取CPU的个数
        System.out.println(Runtime.getRuntime().availableProcessors()+"个");
        //最佳线程数目= （（线程等待时间+线程CPU时间）/线程CPU时间 ）* CPU数目
        //最佳线程数目 = （线程等待时间与线程CPU时间之比 + 1）* CPU数目
        //方式1
        ScheduledExecutorService scheduledExecutorService;

        //

        int corePooSize=2,maxNumPoolSize=4,keepAliveTime=10;
        TimeUnit unit=TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue=new ArrayBlockingQueue<>(2);
        ThreadFactory threadFactory=new NameTreadFactory();
        RejectedExecutionHandler rejectedExecutionHandler=new CustomPolicy();
        //线程池构造
        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(corePooSize,maxNumPoolSize,keepAliveTime,unit,workQueue,threadFactory,rejectedExecutionHandler);
        //预启动所有核心线程
        threadPoolExecutor.prestartAllCoreThreads();
        //任务分配
        for(int i=1;i<=10000;i++){
            objectMap.put("task"+i,"task"+i);
        }
        for(int i=1;i<=10;i++){
            MyTask myTask=new MyTask("task"+i);
            threadPoolExecutor.execute(myTask);
        }
        System.in.read();

    }

    /**
     *
     */
    static class NameTreadFactory implements ThreadFactory{

        private final  AtomicInteger mThreadNum=new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread t=new Thread(r,"test-thread"+ mThreadNum.getAndIncrement());
            System.out.println(t.getName()+"has bean created");
            return t;
        }
    }

    static class CustomPolicy implements RejectedExecutionHandler{

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            doLog(r,executor);
        }
        public void doLog(Runnable r, ThreadPoolExecutor executor){
            //日志记录
            System.out.println(r.toString()+"rejected");
            //System.out.println("completedTaskCount: " + executor.getCompletedTaskCount());
        }

    }

    static class MyTask implements Runnable{

        private Object info;

        /**
         * @Description: TODO
         * @param info 任务信息
         * @return
         * @author fanzj@bonree.com
         * @date 2019/11/5 10:31
         */
        MyTask(Object info){
            this.info=info;
        }

        @Override
        public void run() {
            try {
                System.out.println(this.toString()+"is running!");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        public Object getInfo() {
            return info;
        }

        @Override
        public String toString() {
            return "mytask "+this.info;
        }
    }

}
